package com.platform.consumer;

import cn.hutool.core.date.DateUtil;
import com.jfinal.aop.Duang;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.wxaapp.WxaConfig;
import com.jfinal.wxaapp.WxaConfigKit;
import com.jfinal.wxaapp.api.WxaTemplate;
import com.jfinal.wxaapp.api.WxaTemplateApi;
import com.platform.dao.OrderDao;
import com.platform.dao.OrderGoodsDao;
import com.platform.dao.UserDao;
import com.platform.dao.WxFromidDao;
import com.platform.entity.OrderEntity;
import com.platform.entity.OrderGoodsEntity;
import com.platform.entity.UserEntity;
import com.platform.entity.WxFromidEntity;
import com.platform.utils.ResourceUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.platform.service.impl.OrderServiceImpl.OrderDeliveryReminder;


public class OrderSendGoodsConsumer {

    @Autowired
    private OrderGoodsDao orderGoodsDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private WxFromidDao wxFromidDao;
    @Autowired
    private OrderDao orderDao;


    public void getMessage(Map<String, Object> message) {
        Integer orderId = (Integer) message.get("orderId");
        OrderEntity order = orderDao.queryObject(orderId);


        //发送信息模板
        Integer userId = order.getUserId();
        UserEntity userEntity = userDao.queryObject(userId);
        if (userEntity != null) {
            //todo 获取openid
            String weixinOpenid = userEntity.getWeixinOpenid();

            //todo 获取一个有用的fromid
            Map<String, Object> params = new HashMap<>();
            params.put("openid", weixinOpenid);
            params.put("isUsed", 0);
            params.put("sidx", "add_time");
            params.put("order", "asc");
            List<WxFromidEntity> wxFromidEntities = wxFromidDao.queryList(params);
            if (wxFromidEntities.size() > 0) {
                //todo 配置微信信息
                WxFromidEntity wxFromidEntity = wxFromidEntities.get(0);
                String fromid = wxFromidEntity.getFromid();
                WxaConfig wxaConfig = new WxaConfig();
                wxaConfig.setAppId(ResourceUtil.getConfigByName("wx.appId"));
                wxaConfig.setAppSecret(ResourceUtil.getConfigByName("wx.secret"));
                WxaConfigKit.setWxaConfig(wxaConfig);

                //todo 创建模板信息
                WxaTemplateApi wxaTemplateApi = Duang.duang(WxaTemplateApi.class);
                WxaTemplate template = new WxaTemplate();
                //支付的prepay_id
                template.setForm_id(fromid);
                //接收者（用户）的 openid
                template.setTouser(weixinOpenid);
                //购买成功通知的模板ID
                template.setTemplate_id(OrderDeliveryReminder);
                //小程序调转的页面:订单详情
                template.setPage("pages/orderDetail/orderDetail?orderId=" + order.getId());

                Map<String, Object> orderGoodsParam = new HashMap<>();
                orderGoodsParam.put("order_id", order.getId());
                List<OrderGoodsEntity> orderGoodsVoList = orderGoodsDao.queryList(orderGoodsParam);
                OrderGoodsEntity orderGoodsVo = orderGoodsVoList.get(0);

                //todo 拼接物品名称
                String keyword1 = "";
                if (orderGoodsVo != null) {
                    keyword1 = orderGoodsVo.getGoodsName();
                }
//                if (orderGoodsVoList.size() > 1) {
//                    keyword1 = keyword1 + " 等";
//                }
                //物品名称
                template.add("keyword1", keyword1);
                //快递公司
                template.add("keyword2", order.getShippingName());
                //快递单号
                template.add("keyword3", order.getShippingNo());
                //发货时间
                template.add("keyword4", DateUtil.formatDateTime(order.getDeliveryTime()));
                //订单号
                template.add("keyword5", order.getOrderSn());

                String build = template.build();
                ApiResult send = wxaTemplateApi.send(build);
                Integer errorCode = send.getErrorCode();
                if (errorCode != 0) {
                    System.out.println("模板发送失败信息：" + send.getErrorMsg());
                } else {
                    System.out.println("模板信息发送成功");
                }

            }
        }
    }

}
